|
RAIS
3.2
|
00001 00028 using System; 00029 using System.Data.SqlClient; 00030 using System.Collections.Generic; 00031 using System.Transactions; 00032 using System.Text; 00033 using RAIS.Common.Localization; 00034 using Cultures = System.Collections.Generic.Dictionary<string, RAIS.Common.Localization.Culture>; 00035 using TranslationItems = System.Collections.Generic.Dictionary<string, RAIS.Common.Localization.TranslationItem>; 00036 using Contexts = System.Collections.Generic.Dictionary<string, RAIS.Common.Localization.Context>; 00037 00038 namespace RAIS.DataAccessLayer 00039 { 00043 public class Localisation 00044 { 00045 #region Public static properties 00046 00053 public static Cultures SupportedLanguages 00054 { 00055 get 00056 { 00057 return GetSupportedLanguages(); 00058 } 00059 } 00067 public static Contexts Contexts 00068 { 00069 get 00070 { 00071 return GetContexts(); 00072 } 00073 } 00074 #endregion 00075 #region Public static methods 00076 00086 public static string Translate(string context, string textToTranslate, string sourceLanguage, string targetLanguage) 00087 { 00088 00089 if (string.IsNullOrEmpty(textToTranslate) || (textToTranslate == " | ")) // || sourceLanguage == targetLanguage ) 00090 { 00091 return textToTranslate; 00092 } 00093 else 00094 { 00095 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString)) 00096 { 00097 sqlConnection.Open(); 00098 00099 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetTranslation, sqlConnection)) 00100 { 00101 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure; 00102 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_Context, context); 00103 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TextToTranslate, textToTranslate); 00104 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_SourceLanguage, sourceLanguage); 00105 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TargetLanguage, targetLanguage); 00106 00107 using (SqlDataReader sqlReader = sqlCommand.ExecuteReader()) 00108 { 00109 sqlReader.Read(); 00110 00111 if (sqlReader.HasRows) 00112 return sqlReader["TranslatedText"].ToString(); 00113 else 00114 throw new Exception("(" + textToTranslate + ") " + "cannot be translated into " + "(" + targetLanguage + ")"); 00115 } 00116 } 00117 } 00118 } 00119 } 00127 public static bool CultureExists( String cultureName ) 00128 { 00129 Cultures cultures = GetSupportedLanguages(); 00130 return cultures.ContainsKey(cultureName); 00131 } 00139 public static bool AddCulture(RAIS.Common.Localization.Culture culture, ref string returnMessage) 00140 { 00141 try 00142 { 00143 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString)) 00144 { 00145 sqlConnection.Open(); 00146 00147 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_AddSupportedLanguage, sqlConnection)) 00148 { 00149 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure; 00150 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_SupportedLanguageID, culture.LCID); 00151 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_SupportedLanguageName, culture.Name); 00152 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_SupportedLanguageEnglishName, culture.EnglishName); 00153 sqlCommand.ExecuteNonQuery(); 00154 } 00155 } 00156 returnMessage = RAIS.Common.Messages.CULTURE_ADDED; 00157 return true; 00158 } 00159 catch (Exception excep) 00160 { 00161 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE; 00162 return false; 00163 } 00164 } 00172 public static bool SaveCulture(RAIS.Common.Localization.Culture culture, ref string returnMessage) 00173 { 00174 try 00175 { 00176 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString)) 00177 { 00178 sqlConnection.Open(); 00179 00180 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_UpdateSupportedLanguage, sqlConnection)) 00181 { 00182 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure; 00183 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_SupportedLanguageID, culture.LCID); 00184 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_RightToLeft, culture.RightToLeft); 00185 sqlCommand.ExecuteNonQuery(); 00186 } 00187 } 00188 returnMessage = RAIS.Common.Messages.CULTURE_SAVED; 00189 return true; 00190 } 00191 catch (Exception excep) 00192 { 00193 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE; 00194 return false; 00195 } 00196 } 00204 public static bool DeleteCulture(RAIS.Common.Localization.Culture culture, ref string returnMessage) 00205 { 00206 try 00207 { 00208 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString)) 00209 { 00210 sqlConnection.Open(); 00211 00212 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_DeleteSupportedLanguage, sqlConnection)) 00213 { 00214 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure; 00215 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_SupportedLanguageID, culture.ID); 00216 sqlCommand.ExecuteNonQuery(); 00217 } 00218 } 00219 returnMessage = RAIS.Common.Messages.CULTURE_DELETED; 00220 return true; 00221 } 00222 catch (Exception excep) 00223 { 00224 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE; 00225 return false; 00226 } 00227 } 00233 public static TranslationItems GetAllTranslations(Culture sourceLanguage, Culture targetLanguage, bool untranslated, string sortExpression, string sortDirection) 00234 { 00235 return GetTranslations(null, sourceLanguage, targetLanguage, untranslated, sortExpression, sortDirection); 00236 } 00242 public static TranslationItems GetTranslationsByContext(Context context, Culture sourceLanguage, Culture targetLanguage, bool untranslated, string sortExpression, string sortDirection) 00243 { 00244 return GetTranslations(context, sourceLanguage, targetLanguage, untranslated, sortExpression, sortDirection); 00245 } 00253 public static bool SaveTranslations(TranslationItems translations, ref string returnMessage) 00254 { 00255 bool returnCode = true; 00256 using (TransactionScope ts = new TransactionScope()) 00257 { 00258 int count = 0; 00259 foreach (KeyValuePair<string, TranslationItem> translation in translations) 00260 { 00261 if (translation.Value.Modified) 00262 { 00263 returnCode &= SaveTranslation(translation.Value, ref returnMessage); 00264 00265 if (!returnCode) 00266 break; 00267 else 00268 count++; 00269 } 00270 } 00271 00272 if (returnCode) 00273 { 00274 if( count == 1 ) 00275 returnMessage = string.Format(RAIS.Common.Messages.SAVED_TRANSLATION, count); 00276 else 00277 returnMessage = string.Format(RAIS.Common.Messages.SAVED_TRANSLATIONS, count); 00278 ts.Complete(); 00279 } 00280 } 00281 return returnCode; 00282 } 00283 #endregion 00284 #region Private static helper methods 00285 00290 static Cultures GetSupportedLanguages() 00291 { 00292 Cultures supportedLanguages = new Cultures(); 00293 00294 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString)) 00295 { 00296 sqlConnection.Open(); 00297 00298 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetSupportedLanguages, sqlConnection)) 00299 { 00300 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure; 00301 00302 using (SqlDataReader sqlReader = sqlCommand.ExecuteReader()) 00303 { 00304 while (sqlReader.Read()) 00305 { 00306 Culture culture = new Culture(sqlReader); 00307 supportedLanguages.Add(culture.Name, culture); 00308 } 00309 } 00310 } 00311 } 00312 return supportedLanguages; 00313 } 00319 static Contexts GetContexts() 00320 { 00321 Contexts contexts = new Contexts(); 00322 00323 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString)) 00324 { 00325 sqlConnection.Open(); 00326 00327 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetContexts, sqlConnection)) 00328 { 00329 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure; 00330 00331 using (SqlDataReader sqlReader = sqlCommand.ExecuteReader()) 00332 { 00333 while (sqlReader.Read()) 00334 { 00335 Context context = new Context(sqlReader); 00336 contexts.Add(context.Name, context); 00337 } 00338 } 00339 } 00340 } 00341 return contexts; 00342 } 00348 static TranslationItems GetTranslations(Context context, Culture sourceLanguage, Culture targetLanguage, bool untranslatedOnly, string sortExpression, string sortDirection) 00349 { 00350 TranslationItems translationItems = new TranslationItems(); 00351 00352 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString)) 00353 { 00354 sqlConnection.Open(); 00355 00356 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetTranslations, sqlConnection)) 00357 { 00358 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure; 00359 if (context != null) 00360 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_ContextID, context.ID); 00361 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_SourceLanguage, sourceLanguage.EnglishName); 00362 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TargetLanguage, targetLanguage.EnglishName); 00363 if (untranslatedOnly) 00364 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_Untranslated, untranslatedOnly); 00365 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_SortColumn, sortExpression); 00366 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_SortDirection, sortDirection); 00367 00368 using (SqlDataReader sqlReader = sqlCommand.ExecuteReader()) 00369 { 00370 while (sqlReader.Read()) 00371 { 00372 TranslationItem translationItem = new TranslationItem(sqlReader, sourceLanguage, targetLanguage); 00373 if (!translationItems.ContainsKey(translationItem.Name)) 00374 translationItems.Add(translationItem.Name, translationItem); 00375 } 00376 } 00377 } 00378 } 00379 00380 return translationItems; 00381 } 00389 static bool SaveTranslation(TranslationItem translation, ref string returnMessage) 00390 { 00391 try 00392 { 00393 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString)) 00394 { 00395 sqlConnection.Open(); 00396 00397 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_UpdateTranslation, sqlConnection)) 00398 { 00399 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure; 00400 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TranslationID, translation.ID); 00401 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TargetLanguage, translation.TargetLanguage.EnglishName); 00402 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TranslatedText, translation.TargetText); 00403 sqlCommand.ExecuteNonQuery(); 00404 00405 translation.Modified = false; 00406 } 00407 } 00408 return true; 00409 } 00410 catch (Exception excep) 00411 { 00412 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE; 00413 return false; 00414 } 00415 } 00416 #endregion 00417 } 00418 }